package com.kygl.teacher.dao.imp;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.kygl.teacher.dao.IDeptDao;
import com.kygl.teacher.vo.Dept;

public class DeptDaoImp extends HibernateDaoSupport implements IDeptDao{

	public boolean addDept(Dept t) {
		try {
			this.getHibernateTemplate().save(t);
			return true;

		} catch (RuntimeException re) {
			throw re;
		}
	}

	public boolean delDept(Dept t) {
		
		try {
			this.getHibernateTemplate().delete(t);
			return true;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public boolean delDeptById(int id) {
		try {

			this.getHibernateTemplate().delete(this.getHibernateTemplate().get(
					"com.kygl.teacher.vo.Dept", id));
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Dept> findAllDept() {
		List<Dept> list = this.getHibernateTemplate().loadAll(Dept.class);
		// TODO Auto-generated method stub
		return list;
	}

	public Dept findDeptById(int id) {
		return (Dept) this.getHibernateTemplate().get(
				"com.kygl.teacher.vo.Dept", id);
	}

	@SuppressWarnings("unchecked")
	public boolean findDeptByName(String name) {
		String hql = "from Dept a where a.name=?";

		List<Dept> list = this.getHibernateTemplate().find(hql, name.trim().toLowerCase());

		if (list.isEmpty()) {
			return false;
		} else {
			return true;
		}
	}

	public boolean modifyDept(Dept t) {
		try {
			this.getHibernateTemplate().saveOrUpdate(t);
			return true;
		} catch (RuntimeException re) {

			throw re;
		}
	}

}
